package com.quanshu.exchange.ui.gui.profile;

import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.quanshu.exchange.R;
import com.quanshu.exchange.support.api.ShopInterface;
import com.quanshu.exchange.support.config.Config;
import com.quanshu.exchange.support.entity.FavoriteEntity;
import com.quanshu.exchange.support.net.BaseResponse;
import com.quanshu.exchange.support.net.core.BaseObserver;
import com.quanshu.exchange.support.net.core.BaseTransformer;
import com.quanshu.exchange.support.net.core.NetClient;
import com.quanshu.exchange.support.utils.AppUtils;
import com.quanshu.exchange.support.utils.LogUtil;
import com.quanshu.exchange.support.utils.RxViewHelper;
import com.quanshu.exchange.ui.base.BaseActivity;
import com.quanshu.exchange.ui.base.recycler.BaseViewHolder;
import com.quanshu.exchange.ui.base.recycler.DataDisplayer;
import com.quanshu.exchange.ui.base.recycler.LayoutWrapper;
import com.quanshu.exchange.ui.base.recycler.SingleAdapter;
import com.quanshu.exchange.ui.gui.shop_store.ShopTwoActivity;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 我的收藏
 * Created by xiniu003 on 2018/1/3.
 */

public class MyFavoriteActivity extends BaseActivity {

    @BindView(R.id.back)
    LinearLayout back;
    @BindView(R.id.my_favorite_null)
    LinearLayout myFavoriteNull;
    @BindView(R.id.my_favorite_recy)
    RecyclerView myFavoriteRecy;
    @BindView(R.id.my_favorite_asuya)
    LinearLayout myFavoriteAsuya;

    private List<FavoriteEntity> datas;
    private SingleAdapter<FavoriteEntity> adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_favorite);
        ButterKnife.bind(this);

        initView();
        getFavoList();

        initListener();
    }

    //获取收藏记录
    private void getFavoList() {
        NetClient.getInstance().getShopService(ShopInterface.class)
                .getFavoList(AppUtils.getSignParams(String.valueOf(currentUser.getId())), String.valueOf(currentUser.getId()))
                .compose(new BaseTransformer<>(this))
                .subscribe(new BaseObserver<BaseResponse<List<FavoriteEntity>>>() {
                    @Override
                    public void onSuccess(BaseResponse<List<FavoriteEntity>> response) {

                        if (response.getData() != null && response.getData().size() > 0) {

                            myFavoriteNull.setVisibility(View.GONE);

                            datas = response.getData();
                            adapter.setData(datas);
                        } else {
                            myFavoriteNull.setVisibility(View.VISIBLE);
                        }
                    }

                    @Override
                    public void onFailure(Throwable e) {
                        LogUtil.printException(e);
                    }
                });


    }

    private void initView() {
        myFavoriteRecy.setLayoutManager(new GridLayoutManager(MyFavoriteActivity.this, 2));
        adapter = new SingleAdapter<>(new DataDisplayer<FavoriteEntity>(MyFavoriteActivity.this,
                R.layout.item_my_favorite) {
            @Override
            public void bindData(BaseViewHolder holder,
                                 LayoutWrapper<FavoriteEntity> wrapper,
                                 FavoriteEntity item,
                                 int position) {
                holder.setText(R.id.commodity_name, item.getName());
                holder.setImageUrl(R.id.commodity_image,
                        Config.URL_HOST_SHOP + item.getImg(),
                        R.drawable.default_avatar);
                holder.setText(R.id.pianyi_price, item.getSell_price());
                holder.setText(R.id.gui_price, "￥" + item.getMarket_rmb());

                TextView guiPriceR = holder.getView(R.id.gui_price);
                guiPriceR.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);

                holder.setOnClickListener(R.id.favorite_yes_no, view -> {

                    getDelFavoById(position);
                });
                holder.setOnClickListener(R.id.commodity_image, aVoid -> {
                    Intent intent = new Intent(MyFavoriteActivity.this, ShopTwoActivity.class);
                    String url = Config.URL_HOST_SHOP + "index.php?controller=cattle_exchange_show&action=products&id=" + item.getGood_id();
                    intent.putExtra("url", url);
                    startActivity(intent);
                });
            }
        });
        myFavoriteRecy.setAdapter(adapter);
    }

    //删除一条收藏记录
    private void getDelFavoById(int position) {
        NetClient.getInstance().getShopService(ShopInterface.class)
                .getDelFavoById(AppUtils.getSignParams(datas.get(position).getId(), String.valueOf(currentUser.getId()))
                        , datas.get(position).getId(), String.valueOf(currentUser.getId()))
                .compose(new BaseTransformer<>(this))
                .subscribe(new BaseObserver<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse response) {
                        if (position == datas.size() - 1) {
                            getFavoList();
                            initView();
                        } else {
                            datas.remove(position);
                            adapter.notifyItemRemoved(position);
                            adapter.notifyDataSetChanged();
                        }

                        LogUtil.print(response.getMsg());
                    }

                    @Override
                    public void onFailure(Throwable e) {
                        LogUtil.printException(e);
                    }
                });

    }

    private void initListener() {

        RxViewHelper.getInstance().clicks(back, aVoid -> finish());
    }
}
